package com.xhd.server;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;

import com.xhd.base.log.Log;
import com.xhd.base.log.impl.XLogWriter;
import com.xhd.cache.CacheMgr;
import com.xhd.db.DBCenter;
import com.xhd.db.XDBManager;
import com.xhd.serverComm.DataTranCenter;
import com.xhd.util.ThreadMgr;

public abstract class EventObject {
	public static int ERROR = 1;
	public static int TIME_BEFORE = 2;
	public static int TIME_AFTER = 4;
	public static int EVENT_INIT_SYS = 16;
	public static int EVENT_STOP_SYS = 17;
	public static int EVENT_ACTION_INIT = 32;
	public static int EVENT_ACTION_RELEASE = 64;
	public static int EVENT_DBPOOL_INIT = 128;
	public static int EVENT_LOGIN = 256;
	public static int EVENT_LOGOUT = 512;
	private static final Log logobj;
	static Class obj;

	static {
		Class tmpobj = obj;
		if (tmpobj == null) {
			try {
				tmpobj = (EventObject.obj = Class
						.forName("com.sinocc.base.server.EventObject"));
			} catch (ClassNotFoundException localClassNotFoundException) {
				throw new NoClassDefFoundError(
						localClassNotFoundException.getMessage());
			}
		}
		logobj = LogFactory.getLog(tmpobj);
	}

	private void doLogAction(int paramInt, Object paramObject) {
		if ((paramInt == EVENT_LOGIN) || (paramInt != EVENT_LOGOUT)
				|| (ServerConfig.ServerType != 1))
			return;
		CPCServerId localCPCServerId = CPCServerId.getLocal(2);
		DataTranCenter.addSendData(localCPCServerId.getId(), "<logout>",
				(String) paramObject);
	}

	private void shutdown() {
		if (4 == ServerConfig.SystemState)
			return;
		ServerConfig.SystemState = 4;
		System.out.println("Shoutdow...");
		logobj.info("Shoutdow...");
		AfterStop();
		try {
			XDBManager.getInstance().release();
			ThreadMgr.stopThreads();
			XLogWriter.getInstance().freeFileWriter();
			CacheMgr.clearCaches();
		} catch (Exception localException) {
			localException.printStackTrace();
		}
		System.out.println("Shoutdow OK.");
	}

	public void onEvent(int paramInt) {
		onEvent(paramInt, null, null);
	}

	public void onEvent(int paramInt, Object paramObject) {
		onEvent(paramInt, paramObject, null);
	}

	public void onEvent(int paramInt, Object paramObject1, Object paramObject2) {
		try {
			if ((paramInt & EVENT_LOGIN) == EVENT_LOGIN) {
				doLogAction(EVENT_LOGIN, paramObject1);
				return;
			}
			if ((paramInt & EVENT_LOGOUT) == EVENT_LOGOUT) {
				doLogAction(EVENT_LOGOUT, paramObject1);
				return;
			}
			if ((paramInt & TIME_BEFORE) == TIME_BEFORE) {
				int i = paramInt & (TIME_BEFORE ^ 0xFFFFFFFF);
				if (i == EVENT_INIT_SYS) {
					BeforeStart();
					return;
				}
				BeforeEvent(i, paramObject1, paramObject2);
				return;
			}
			if ((paramInt & TIME_AFTER) != TIME_AFTER)
				return;
			int i = paramInt & (TIME_AFTER ^ 0xFFFFFFFF);
			if (i == EVENT_INIT_SYS) {
				AfterStart();
				return;
			}
			if (i == EVENT_STOP_SYS) {
				shutdown();
				return;
			}
			AfterEvent(i, paramObject1, paramObject2);
		} catch (Throwable localThrowable) {
			logobj.error(localThrowable);
		}
	}

	protected abstract boolean BeforeEvent(int paramInt, Object paramObject1,
			Object paramObject2);

	protected abstract boolean AfterEvent(int paramInt, Object paramObject1,
			Object paramObject2);

	protected abstract boolean BeforeStart();

	protected abstract boolean AfterStart();

	protected abstract boolean AfterStop();
}
